package com.dataart.productshop.controller;

import com.dataart.productshop.Constants;
import com.dataart.productshop.model.boiko.informationsystem.*;
import com.dataart.productshop.service.GpaManager;
import com.dataart.productshop.service.LecturerManager;
import com.dataart.productshop.service.StudentManager;
import com.dataart.productshop.service.SubjectManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class GpaController {

    @Autowired
    private GpaManager gpaManager;

    @Autowired
    private LecturerManager lecturerManager;

    @Autowired
    private SubjectManager subjectManager;

    @Autowired
    StudentManager studentManager;

    @RequestMapping(value = "/gpa", method = RequestMethod.GET)
    public ModelAndView showAllGpa(){
        return new ModelAndView("gpa", "items", gpaManager.getAll());
    }

    @RequestMapping(value = "/gpa/addnew", method = RequestMethod.GET)
    public ModelAndView addNewGpa(){

        Map<String, Object> bigMap = new HashMap<String, Object>();
        bigMap.put("studentList", studentManager.getAll());
        bigMap.put("lecturerList", lecturerManager.getAll());
        bigMap.put("subjectList", subjectManager.getAll());
        return new ModelAndView("addGpa", bigMap);
    }

    @RequestMapping(value = {"/gpa/addnew",
            "/edit" + Constants.GPA_TYPE + "/{itemId}"},
            method = RequestMethod.POST)
    public String createNewGpa(
            @RequestParam(value = "student", required = false)String studentId,
            @RequestParam(value = "id", required = false)String id,
            @RequestParam(value = "lecturer", required = false)String lecturerId,
            @RequestParam(value = "subject", required = false) String subjectId,
            @RequestParam(value = "gpa", required = false)String gpa){
        if(studentId != null && lecturerId != null && subjectId != null
                && gpa != null){
            try{
                Student st = new Student();
                st.setId(Long.parseLong(studentId));

                Lecturer lecturer = new Lecturer();
                lecturer.setId(Long.parseLong(lecturerId));

                Subject subject = new Subject();
                subject.setId(Long.parseLong(subjectId));

                Double gpaDouble = Double.parseDouble(gpa);

                Gpa newGpa = new Gpa();
                if(id != null && !id.isEmpty()){
                    newGpa = gpaManager.getById(Long.parseLong(id));
                }
                newGpa.setGpa(gpaDouble);
                newGpa.setStudent(st);
                newGpa.setLecturer(lecturer);
                newGpa.setSubject(subject);

                gpaManager.save(newGpa);
            }catch(NumberFormatException ex){
                System.err.println("NumberFormatException");
                return "redirect:/gpa/addnew";
            }

        }
        return "redirect:/gpa";
    }

    @RequestMapping(value = "/remove" + Constants.GPA_TYPE + "/{itemId}"
            , method = RequestMethod.GET)
    public String removeStudent(@PathVariable("itemId") String itemId){

        try{
            gpaManager.delete(Long.parseLong(itemId));
        }catch (NumberFormatException e){

        }catch(Exception dd){
            System.err.println("some exception were occured in AssKathedra controller"
                    + dd.getStackTrace());
        }
        return "redirect:/gpa";

    }




}
